package ysq.xyg.web.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.StringUtil;
import org.hibernate.validator.constraints.NotBlank;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.base.aspect.BaseLog;
import ysq.xyg.base.controller.BaseController;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.base.util.BaseUtil;
import ysq.xyg.base.util.EasyPoiUtil;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.web.model.po.WebMember;
import ysq.xyg.web.model.vo.WebMemberExportVo;
import ysq.xyg.web.model.vo.WebMemberVo;
import ysq.xyg.web.service.WebMemberService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 业务模块-会员controller层
 *
 * @author shengQiang yu
 * @date 2019-4-26 13:51:56
 */
@RestController
public class WebMemberController extends BaseController {

    private static Logger log= LoggerFactory.getLogger(WebMemberController.class);
    @Resource
    private WebMemberService webMemberService;

    private WebMember webMember;

    /**
     * 分页查询
     *
     * @param webMemberVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @author shengQiang yu
     * @date 2019-4-26 13:51:56
     */
    @BaseLog(module = "业务模块",className = "会员",methods = "分页查询",businessType = BaseConstant.SYS_BUSINESS_TYPE_GET,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/webMember/page",method = RequestMethod.POST)
    public ReturnEntity page(HttpServletRequest request, WebMemberVo webMemberVo, int pageSize, int pageNum){
        // 如果不是管理员，则必须要使用accountId
        SysUser sysUser = acceptCurrentUser(request);
        webMemberVo.setAccountId(sysUser.getAccountId());
        ReturnEntity returnEntity = webMemberService.page(webMemberVo,pageSize,pageNum);
        return returnEntity;
    }

    /**
     * 跳转连接
     *
     * @param id 主键id
     * @author shengQiang yu
     * @date 2019-4-26 13:51:56
     */
    @RequestMapping(value = "/webMember/getEntity",method = RequestMethod.GET)
    public ModelAndView getEntity(String id){
        ModelAndView view = new ModelAndView("/web/webMemberUpdateOrSave");
        if(StrUtil.isNotEmpty(id)){
            webMember = webMemberService.selectByPrimaryKey(id);
            view.addObject("webMember", webMember);
        }
        return view;
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @author shengQiang yu
     * @date 2019-4-26 13:51:56
     */
    @BaseLog(module = "业务模块",className = "会员",methods = "删除",businessType = BaseConstant.SYS_BUSINESS_TYPE_DELETE,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/webMember/delete")
    public ReturnEntity delete(HttpServletRequest request,String id){
        return webMemberService.delete(id,acceptCurrentUser(request));
    }

    /**
     * 添加或修改
     *
     * @param webMemberVo Vo对象
     * @author shengQiang yu
     * @date 2019-4-26 13:51:56
     */
    @BaseLog(module = "业务模块",className = "会员",methods = "添加或修改",businessType = BaseConstant.SYS_BUSINESS_TYPE_UPDATE,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/webMember/addOrUpdate",method = RequestMethod.POST)
    public ReturnEntity delete(HttpServletRequest request,WebMemberVo webMemberVo){
        return webMemberService.addOrUpdate(webMemberVo,acceptCurrentUser(request));
    }

    /**
     * 导出
     * @param response
     * @param webMemberVo
     */
    @BaseLog(module = "业务模块",className = "会员",methods = "导出",businessType = BaseConstant.SYS_BUSINESS_TYPE_UPDATE,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping("/webMember/export")
    public void export(HttpServletResponse response, WebMemberVo webMemberVo){
        if(StringUtil.isNotEmpty(webMemberVo.getNickName())){
            webMemberVo.setNickName(BaseUtil.encodeBase64String(webMemberVo.getNickName()));
        }
        List<WebMember> memberList = webMemberService.acceptList(webMemberVo);
        List<WebMemberExportVo> webMemberExportVoList = new ArrayList<WebMemberExportVo>();
        if (memberList != null && memberList.size() > 0) {
            for (WebMember webMember : memberList) {
                WebMemberExportVo webMemberExportVo = new WebMemberExportVo();
                String nickName = BaseUtil.decodeBase64(webMember.getNickName());
                BeanUtil.copyProperties(webMember, webMemberExportVo);
                webMemberExportVo.setNickName(nickName);
                // bean复制
                webMemberExportVoList.add(webMemberExportVo);
            }
        }
        String nowDate = DateUtil.formatDate(DateUtil.date());
        // 导出操作
        EasyPoiUtil.exportExcel(webMemberExportVoList, nowDate + "会员展示", "订单", WebMemberExportVo.class, nowDate + "会员展示.xls", response);
    }

    /**
     * 查询回收权限人员列表
     * @param code
     * @author congcong tao
     * @date 2019/7/9 14:36
     */
    @RequestMapping(value = "/webMember/acceptRecoveryPermissionSelect", method = RequestMethod.POST)
    public ReturnEntity acceptRecoveryPermissionSelect(String code){
        return webMemberService.acceptRecoveryPermissionSelect(code);
    }

    @RequestMapping(value = "/webMember/acceptGradeTwoSelect", method = RequestMethod.POST)
    public ReturnEntity acceptGradeTwoSelect(String code){
        return webMemberService.acceptGradeTwoSelect(code);
    }

    @RequestMapping(value = "/webMember/selectEntityByCardNo", method = RequestMethod.POST)
    public ReturnEntity selectEntityByCardNo(String cardNo){
        WebMember webMember = webMemberService.selectEntityByCardNo(cardNo);
        if(StrUtil.isNotEmpty(webMember.getNickName())){
            webMember.setNickName(BaseUtil.decodeBase64(webMember.getNickName()));
        }
        return new ReturnEntity(webMember);
    }

    @RequestMapping(value = "/webMember/updateScore", method = RequestMethod.POST)
    public ReturnEntity updateScore(String id, String score, String description){
        return webMemberService.updateScore(id, score, description);
    }

    @RequestMapping(value = "/webMember/setGrade", method = RequestMethod.POST)
    public ReturnEntity updateScore(@NotBlank(message = "主键不能为空") String id, @NotBlank(message = "级别不能为空")String grade,
                                    @NotBlank(message = "姓名不能为空") String name, @NotBlank(message = "手机号不能为空") String phone){
        WebMember webMember = new WebMember();
        webMember.setId(id);
        webMember.setGrade(grade);
        webMember.setName(name);
        webMember.setPhone(phone);
        webMemberService.updateByPrimaryKeySelective(webMember);
        return new ReturnEntity();
    }


}